<?php

namespace app\service\city;

use app\service\base\BaseLogic;

class CityLogic extends BaseLogic
{
    private $key = '7b5126426e263e07885584d43a872d35';
    private $restApi = 'https://restapi.amap.com/v3/config/district';

    protected $arrLevel = [
        'province' => 1, // 省份（直辖市会在province显示）
        'city' => 2, //市（直辖市会在province显示）
        'district' => 3, //区县
        'street' => 4 //街道
    ];

    /**
     * 省份
     */
    public function curlProvince()
    {
        // 高德地图api
        $arrSend = [
            'key' => $this->key,
            'keywords' => '中国',
        ];
        $json = curl_url($this->restApi, 'get', $arrSend);
        $arrData = json_decode($json, true);
        foreach ($arrData['districts'][0]['districts'] as $arrVal) {
            echo $arrVal['name'] . PHP_EOL;
            $this->curlProvinceSub($arrVal['name'], 0);
        }
    }

    /**
     * 省份下的所有区域
     * @param $keyword
     * @param $parentId
     */
    public function curlProvinceSub($keyword = '', $parentId = 0)
    {
        // 高德地图api
        $arrSend = [
            'key' => $this->key,
            'subdistrict' => 4,
            'keywords' => $keyword,
//            'keywords' => '贵州省',
        ];
        $json = curl_url($this->restApi, 'get', $arrSend);
        $arrData = json_decode($json, true);
        try {
            foreach ($arrData['districts']  as $arrVal) {
                echo $arrVal['name'] . PHP_EOL;
                // 市
                $cityId = $this->add($this->format($arrVal, $parentId));
                if (empty($arrVal['districts'])) {
                    continue;
                }
                foreach ($arrVal['districts'] as $arrItem) {
                    echo '  ' . $arrItem['name'] . PHP_EOL;
                    // 县
                    $districtId = $this->add($this->format($arrItem, $cityId));
                    if (empty($arrItem['districts'])) {
                        continue;
                    }
                    foreach ($arrItem['districts'] as $arrTemp) {
                        echo '    ' . $arrTemp['name'] . PHP_EOL;
                        // 街道
                        $streetId = $this->add($this->format($arrTemp, $districtId));
                        if (empty($arrTemp['districts'])) {
                            continue;
                        }
                        foreach ($arrTemp['districts'] as $arrFinal) {
                            echo '      ' . $arrFinal['name'] . PHP_EOL;
                            $this->add($this->format($arrFinal, $streetId));
                        }
                    }
                }
            }
        } catch (\Exception $e) {
            dd($e->getLine(), $e->getMessage());
        }
    }

    private function format($arrVal, $parentId)
    {
        return [
            'name'      => $arrVal['name'],
            'center'    => $arrVal['center'],
            'level'     => $this->arrLevel[$arrVal['level']],
            'parent_id' => $parentId,
            'city_code' => !empty($arrVal['citycode']) ? $arrVal['citycode'] : '',
            'ad_code'   => !empty($arrVal['adcode']) ? $arrVal['adcode'] : '',
        ];
    }

    private function add($arrInsert)
    {
        return (new CityModel())->add($arrInsert);
    }
}